package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.AdvertDAO;
import com.hcrazy.manchester.app.jdbc.LiveAddressDAO;
import com.hcrazy.manchester.app.pojo.Advert;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.LiveAddress;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class LiveAddressService {

	private LiveAddressDAO liveAddressDAO;
	
	private AdvertDAO advertDAO;
	
	public AdvertDAO getAdvertDAO() {
		return advertDAO;
	}

	public void setAdvertDAO(AdvertDAO advertDAO) {
		this.advertDAO = advertDAO;
	}

	public LiveAddressDAO getLiveAddressDAO() {
		return liveAddressDAO;
	}

	public void setLiveAddressDAO(LiveAddressDAO liveAddressDAO) {
		this.liveAddressDAO = liveAddressDAO;
	}
	
	

	public ErrorMsg addOrUpdateLiveAddress(LiveAddress common) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		long infoId = common.getInfoId();
		if(common.getInfoId()>0){
			infoId = common.getInfoId();
			common.setPraiseCount(common.getPraiseCount() + common.getPraiseBaseCount());
			common.setCommentCount(common.getCommentCount() + common.getCommentBaseCount());
			
			liveAddressDAO.updateLiveAddress(common);
		}else{
			if(null != common && common.getPraiseBaseCount() > 0){
				common.setPraiseCount(common.getPraiseBaseCount());
			}
			if(null != common && common.getCommentBaseCount() > 0){
				common.setCommentCount(common.getCommentBaseCount());
			}
			infoId = liveAddressDAO.addLiveAddress(common);
		}
		
		if(common.getAdvertStr() != null && !common.getAdvertStr().equals("")){
			Gson gson = new Gson();
			Type type = new TypeToken<Advert>(){}.getType();
			Advert ad = gson.fromJson(common.getAdvertStr(),type);
			if(null != ad && ad.getAdContent() != null && !ad.getAdContent().equals("")){
				advertDAO.deleteAdvert(infoId,Constant.COMM_ADDRESS_INFO_MODULE);
				
				ad.setModuleId(infoId);
				ad.setModuleType(Constant.COMM_ADDRESS_INFO_MODULE);
				advertDAO.addAdvert(ad);
			}
			
		}
		
		return msg;
	}
	
	public PageModel getAddressInfos(PageModel model,String search,int sortId) throws Exception{
		return liveAddressDAO.getPagedInfos(model, search, sortId);
	}
	
	public LiveAddress getInfoById(int infoId) throws Exception{
		LiveAddress address = liveAddressDAO.getById(infoId);
		Advert advert = advertDAO.getAdvert(infoId,Constant.COMM_ADDRESS_INFO_MODULE);
		if(null != advert)
			address.setAdvert(advert);
		return address;
	}
	
	public void delLiveAddress(int infoId) throws Exception{
		liveAddressDAO.delLiveAddress(infoId);
	}
	
	/**
	 * 获取大分类下的商户列表（如：我是吃货等）
	 * @param parentSortId
	 * @return
	 * @throws Exception
	 */
	public List<LiveAddress> getAddressInfos(int parentSortId,int size) throws Exception{
		return liveAddressDAO.getLiveAddresses(parentSortId,size);
	}
}
